﻿using BonjourQuotation.DataAccess.Entity_Framework;
using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.BusinessData;
using BonjourQuotation.ObjectModel.ErrorData;
using BonjourQuotation.ObjectModel.LoggerData;
using BonjourQuotation.ObjectModel.SeasonData;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BonjourQuotation.DataAccess.Repositories
{
    public class SeasonRepository : ISeasonRepository
    {
        private readonly BonDataEntities _context = new BonDataEntities();
        private readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public ErrorBInfo CreateSeason(ManageSeasonsGridRowBData SeasonData)
        {
            ErrorBInfo Error = new ErrorBInfo();
            try
            {
                int maxSeasonId = _context.tblSeasons.Select(Season => Season.SeasonID).Max();
                tblSeason NewSeason = new tblSeason()
                {
                    SeasonID = maxSeasonId+1,
                    SeasonCode = SeasonData.SeasonCode,
                    SeasonDescription = SeasonData.SeasonDescription,
                    StartDate = SeasonData.StartDate,
                    FinishDate = SeasonData.FinishDate,
                    IsCurrent = SeasonData.IsCurrent,
                };
                _context.tblSeasons.Add(NewSeason);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                Error.errorCode = 100;
                Error.Message = ex.Message;
                logger.Error(ex.ToMessageAndCompleteStacktrace());
            }
            return Error;
        }
        public ManageSeasonsGridBData LoadSeasons(int PageNumber)
        {
            ManageSeasonsGridBData Seasons = new ManageSeasonsGridBData();
            Seasons.Rows = new List<ManageSeasonsGridRowBData>();
            Seasons.Rows = (from Season in _context.tblSeasons
                            orderby Season.SeasonCode
                            select new ManageSeasonsGridRowBData()
                              {
                                  SeasonID = Season.SeasonID,
                                  SeasonCode = Season.SeasonCode,
                                  SeasonDescription = Season.SeasonDescription,
                                  StartDate=Season.StartDate,
                                  FinishDate=Season.FinishDate,
                                  IsCurrent = Season.IsCurrent,
                              }).Take(PageNumber * ManageSeasonsGridBData.PageCount).ToList();
            Seasons.IsCurrent = new Dictionary<bool, string>
            {
                {true,"Yes"},
                {false,"No"},
            };
            return Seasons;
        }
        public ManageSeasonsGridBData LoadSearchedSeasons(ManageSeasonsGridBData Data)
        {
            var FilterSeason = _context.tblSeasons.AsQueryable();
            if (Data.SeasonCode != null && Data.SeasonCode.Length > 0)
                FilterSeason = FilterSeason.Where(Season => Season.SeasonCode.Contains(Data.SeasonCode.Trim()));
            if (Data.SeasonDescription != null && Data.SeasonDescription.Length > 0)
                FilterSeason = FilterSeason.Where(Season => Season.SeasonDescription.Contains(Data.SeasonDescription.Trim()));
            if (Data.SelectedIsCurrent != null)
                FilterSeason = FilterSeason.Where(Season => Season.IsCurrent == Data.SelectedIsCurrent);
            ManageSeasonsGridBData AllSeasons;
            AllSeasons = new ManageSeasonsGridBData();
            AllSeasons.Rows = new List<ManageSeasonsGridRowBData>();
            AllSeasons.Rows = (from Season in FilterSeason
                               orderby Season.SeasonCode
                               select new ManageSeasonsGridRowBData()
                                 {
                                     SeasonID = Season.SeasonID,
                                     SeasonCode = Season.SeasonCode,
                                     SeasonDescription = Season.SeasonDescription,
                                     StartDate=Season.StartDate,
                                     FinishDate=Season.FinishDate,
                                     IsCurrent = Season.IsCurrent,
                                 }).Take(Data.PageNum * ManageSeasonsGridBData.PageCount).ToList();
            AllSeasons.IsCurrent = new Dictionary<bool, string>
            {
               {true,"Yes"},
               {false,"No"},
            };
            return AllSeasons;
        }
        public ErrorBInfo SaveSeasonDetailsData(ManageSeasonsGridBData Data)
        {
            ErrorBInfo Error = new ErrorBInfo();
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var UpdateIsCurrent = _context.tblSeasons.ToList();
                    foreach (var Season in UpdateIsCurrent)
                    {
                        Season.IsCurrent = false;
                    }
                    _context.SaveChanges();
                    List<int> SeasonIDs = Data.Rows.Select(Season => Season.SeasonID).ToList();
                    var SaveEntitySeasons = _context.tblSeasons.Where(Season => SeasonIDs.Contains(Season.SeasonID)).ToList();
                    foreach (var Season in SaveEntitySeasons)
                    {
                        var SaveData = Data.Rows.Where(data => data.SeasonID == Season.SeasonID).SingleOrDefault();
                        if (SaveData != null)
                        {
                            Season.SeasonCode = SaveData.SeasonCode;
                            Season.SeasonDescription = SaveData.SeasonDescription;
                            Season.StartDate = SaveData.StartDate;
                            Season.FinishDate = SaveData.FinishDate;
                            Season.IsCurrent = SaveData.IsCurrent;
                        }
                    }
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Error.errorCode = 100;
                    Error.Message = ex.Message;
                    logger.Error(ex.ToMessageAndCompleteStacktrace());
                    transaction.Rollback();
                }
            }
            return Error;
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
